package cn.krui.project.service.system;

import cn.krui.helper.bean.DataArray;
import cn.krui.helper.bean.DataSingle;
import cn.krui.helper.db.DBHelper;
import cn.krui.helper.db.SQL;
import cn.krui.helper.db.SQL.UpdateSQL;
import cn.krui.helper.tools.MapUtils;
import cn.krui.project.bean.system.sys_role.sys_role;
import cn.krui.project.controller.system.SystemBase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 

/**
 * @program: project-system
 * @description: 角色模块
 * @author: admin
 * @create: 2019-10-11 10:57
 **/
@Service
public class SysRoleService extends SystemBase {

	@Autowired
	private DBHelper dbHelper;

	/**
	 * 查询系统角色
	 */
	public DataArray<sys_role> selectSysRoleList(DataSingle<sys_role> sysRole) {
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_role");
		Map<String, Object> where = new HashMap<>(4);
		if (sysRole.getExtend() != null) {
			// 修改自己的权限，只能看到已经绑定的权限，修改自建用户的可以看到用户父级的
			Map<String, Object> o = dbHelper.selectOne("group_id,role_id", "qr_sys_users",
					"id=" + sysRole.getExtend().toString(), null);
			if (getUser().getUid() == (int) sysRole.getExtend()) {
				where.put("role_id", o.get("role_id"));
				sql.where("id=#{role_id}");
			} else {
				where.put("group_id", o.get("group_id"));
				sql.where("(group_id=#{group_id})");
			}
		} else {
			where.put("group_id", getUser().getUid());
			sql.where("(group_id=#{group_id})");
		}
		if (!StringUtils.isEmpty(sysRole.getDataSingle().getDept_id())) {
			where.put("dept_id", sysRole.getDataSingle().getDept_id());
			sql.where("dept_id=#{dept_id}");
		}
		if (!StringUtils.isEmpty(sysRole.getDataSingle().getRole_name())) {
			where.put("role_name", sysRole.getDataSingle().getRole_name());
			sql.where("role_name like '%#{role_name}%'");
		}
		sql.orderBy("sort desc");
		sql.page(sysRole.getPageIndex(), sysRole.getPageSize());
		List<sys_role> sysRolesList = dbHelper.select(sql, where, sys_role.class);
		// where.clear();
		DataArray<sys_role> list = new DataArray<>();
		list.setDataList(sysRolesList);
		sql.clearColumns();
		sql.clearOrderBy();
		sql.select("count(*) as cnt").rows(null).offset(null);
		Map<String, Object> result = dbHelper.selectOne(sql);
		list.setDataCount(MapUtils.getInt(result, "cnt", 0));
		return list;
	}

	/**
	 * 添加系统角色
	 * 
	 * @param sysRole
	 * @return
	 */
	public Integer addSysRole(sys_role sysRole) {
		Object i = dbHelper.insert("qr_sys_role", sysRole, sys_role.class);
		return Integer.valueOf(i.toString());
	}

	/**
	 * 更新系统角色
	 * 
	 * @type 0=更新全部 1=更新角色 2=更新权限
	 * @return
	 */
	public Integer updateSysRole(sys_role sysRole, int type) {
		int i = 0;
		if (type == 0) {
			UpdateSQL sql = SQL.update("qr_sys_role").addField("role_name", sysRole.getRole_name())
					.addField("user_type", sysRole.getUser_type()).where("id=#{id}")
					.addParameter("id", sysRole.getId());
			i = dbHelper.update(sql);
		} else if (type == 1) {
			UpdateSQL sql = SQL.update("qr_sys_role").addField("role_menu", sysRole.getRole_menu()).where("id=#{id}")
					.addParameter("id", sysRole.getId());
			i = dbHelper.update(sql);
		} else if (type == 2) {
			UpdateSQL sql = SQL.update("qr_sys_role").addField("role_permissions", sysRole.getRole_permissions())
					.where("id=#{id}").addParameter("id", sysRole.getId());
			i = dbHelper.update(sql);
		}
		return i;
	}

	/**
	 * 删除系统角色
	 * 
	 * @return
	 */
	public Integer deleteSysRole(sys_role sysRole) {
		SQL.DeleteSQL sql = SQL.delete("qr_sys_role").where("id=#{id}").addParameter("id", sysRole.getId())
				.addParameter("init_default", 0);
		int i = dbHelper.delete(sql);
		return i;
	}

}
